<?php $Config = Config::getInstance();
$webSite = $Config->getWebSiteConfig();
?>
<style>.self_rider{display:none;}</style>
<div class="modal fade" id="setting" tabindex="-1" role="dialog" >
    <div class="modal-dialog" role="document">
        <form class="form-ajax" action="?md=shop&cl=shop&at=settingExpress"
              method="post" autocomplete="off">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">订单指派</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="order_id" name="order_id"/>
                    <div class="form-group">
                        <label for="recipient-name" class="control-label">配送方:</label>
                        <?php
                        $express_type =Data::$_express_type_shop ;
                        unset($express_type['no']);
                        echo Tools::selectOptions("express_type", $express_type, '--请选择--', true, '', 'class="form-control select" required onchange="change()" ');
                        ?>
                    </div>
                    <div class="form-group self_rider" >
                        <label for="message-text" class="control-label">指定骑手:</label>
                        <select name="riderName" id="" class="form-control select" >
                            <option value="" >请选择...</option>
                           <?php 
						   if(!empty($ridersList)){
							   foreach($ridersList as $rd){
								   echo '  <option value="'.$rd['id'].'_'.$rd['name'].'_'.$rd['tel'].'" >'.$rd['name'].'</option>';
							   }
						   }
						   ?>
                        </select>
                    </div>

                    <div class="form-group self_rider">
                        <label for="message-text" class="control-label">骑手费用:</label>
                        <input type="text" id="shop_rider_fee" name ="shop_rider_fee" class="form-control"  placeholder="" value="" />
                    </div>

                    <div class="form-group">
                        <label for="message-text" class="control-label">预计送达时间:</label>
                        <input type="text" id="shop_delivery_time" name ="shop_delivery_time" class="form-control form_datetime_hour" onchange="deliveryTimeChange()" placeholder="" value="" required/>
                        <div><label class="red" id="preinstall"> </label></div>
                    </div>

<!--                    骑手需要      -->
                    <div class="form-group" id="pickup_time" style="display: none">
                        <label for="message-text" class="control-label">预计取货时间:</label>
                        <input type="text" id="shop_pickup_time" name ="shop_pickup_time" class="form-control form_datetime_hour" placeholder="" value="" required/>
                        <div><label class="red">预设抓"预计送达时间"的前30分钟</label></div>
                    </div>


                    <div class="form-group" id="jrd_type" style="display: none">
                        <label for="message-text" class="control-label">预约骑手类型:</label>
<!--                        <input type="text" id="shop_jrd_type" name ="jrd_type" class="form-control form_datetime_hour" placeholder="" value="" />-->
                        <?php
                        echo Tools::selectOptions("jrd_type", Data::$jrd_type, '--请选择--', true, '', 'class="form-control select"');
                        ?>
                    </div>

                    <div class="form-group" id="is_direct_delivery" style="display: none">
                        <label for="message-text" class="control-label">配送方式：</label>
                        <div>
                            <?php
                                foreach (Data::$_dd_direct_delivery as $k=>$dd){
                                    if($k == 0)
                                        $ck = 'checked';
                                    else
                                        $ck = '';
                                    echo '<label style=" margin-right:20px;"><input name="is_direct_delivery" type="radio" value="'.$k.'" '.$ck.'>'.$dd.'</label>';
                                }
                            ?>
                        </div>
                    </div>

                    <div class="form-group" id="sf_push_type" style="display: none">
                        <label for="message-text" class="control-label">商品品类:</label>

                        <?php
                        foreach (Data::$_sf_push_type_for_hunan as $k=>$dd){
                            if($k == 99)
                                $ck = 'checked';
                            else
                                $ck = '';
                            echo '<label style=" margin-right:20px;"><input name="sf_push_type" type="radio" value="'.$k.'" '.$ck.'>'.$dd.'</label>';
                        }
                        ?>

                    </div>

                    <div class="form-group">
                        <label for="message-text" class="control-label">商品实际重量（单位：kg）:</label>
                        <input type="text" id="shop_weight_gram" name ="shop_weight_gram" class="form-control " placeholder="" value="" required />
                        <div><label class="red">实际重量不得超过20kg</label></div>
                    </div>

                    <div class="form-group">
                        <label for="message-text" class="control-label">配送小费:</label>
                        <div>
                            <select class="form-control m-b" name="tip" id="tip">
                                <option value="">小费选择...</option>
                                <?php
                                foreach (Data::$_tip as $k => $tip){
                                    echo '<option value="'.$k.'">'.$tip.'</option>';
                                }

                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="message-text" class="control-label">配送小费备注:</label>
                        <input type="text" id="tip_remark" name ="tip_remark" class="form-control " placeholder="" value="" />
                    </div>

                    <div class="form-group">
                        <label for="message-text" class="control-label">指派备注:</label>
                        <input type="text" id="shop_express_remark" name ="shop_express_remark" class="form-control " placeholder="" value="" />
                    </div>
                    
					<div><label><input name="notice_consignee" type="checkbox" value="1" style=" margin:2px 5px 0 0" id="notice_consignee">不通知收货人</label></div>
                    
                    <div class="form-group" id="distance"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="submit">提交</button>
                    <?php include_once MvcReg::$_moduleName . '/views/layout/woody-prompt.php'; ?>
                </div>

            </div>
        </form>
    </div>
</div>
<link href="webroot/common/bootstrapValidator.css?v=<?php echo constant('JSCSS_VERSION')?>" type="text/css" rel="stylesheet">
<script src="webroot/common/bootstrapValidator.js?v=<?php echo constant('JSCSS_VERSION')?>" type="text/javascript"></script>
<script>

    var Gong = {
        StringLength:GetLength,
    };
    function GetLength(str){
        var realLength = 0, len = str.length, charCode = -1;
        for (var i = 0; i < len; i++) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128)
                realLength += 1;
            else
                realLength += 2;
        }
        return realLength;
    }


    function settings(id, delivery_time, weight,distance,p_ids,source,shop_express_remark){
        $("#order_id").val(id);
        var dt=new Date(delivery_time);
        $("#shop_delivery_time").val(dt.Format('yyyy-MM-dd hh:mm'));
        dt.setMinutes(dt.getMinutes()-30);
        var dt2=dt.Format('yyyy-MM-dd hh:mm');
        

        if(source != '饿了么' && source != '美团' ){
            // 计算商品实际重量
           // GetWeight(p_ids);
           // $("#shop_weight_gram").attr('readonly','readonly');
        }else{
           // $("#shop_weight_gram").removeAttr('readonly');
        };
        if(distance > 7){
            $("#distance").css('color','red');
        }else{
            $("#distance").css('color','black');
        }
        $("#distance").html('预估公里数 :'+distance + '&nbsp;km');

        $("#shop_express_remark").val(shop_express_remark);
    }


    function GetWeight(p_ids){
        $.ajax({
            url:"?md=shop&cl=shop&at=GetWeight",
            dataType:"json",
            data:{p_ids:p_ids},
            type:"post",
            success:function (d) {
                if(d.status == 1){
                    $("#shop_weight_gram").val(d.w);
                }else{
                    $("#shop_weight_gram").val('');
                    alert(d.msg);
                    return false;
                }
            }
        })
    }

    $(document).ready(function() {
        $('#setting').bootstrapValidator({
            message: '此栏位值格式不对',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                'express_type': {
                    validators: {
                        notEmpty: {
                            message: '此栏位不能为空'
                        },
                    }
                },
                'shop_delivery_time': {
                    validators: {
                        notEmpty: {
                            message: '此栏位不能为空'
                        },
                        date: {
                            format: 'YYYY-MM-DD HH:mm',
                            message: '此栏位值格式不对'
                        },
                    }
                },

                'shop_rider_fee': {
                    validators: {
                        notEmpty: {
                            message: '此栏位不能为空'
                        },
                    }
                },
                'shop_weight_gram': {
                    validators: {
                        notEmpty: {
                            message: '此栏位不能为空'
                        },
                        regexp: {
                            regexp: /(^[1-9][0-9]*[\.]{0,1}[0-9]*[0-9]$)|(^[0].[0-9]*[0-9]$)|(^[0-9]$)/,
                            message: '此栏位值格式不对'
                        },
                        callback: {
                            message: '实际重量不得超过20kg',
                            callback: function(value, validator) {
                                if(value != ''){
                                    var w=parseFloat(value);
                                    if (w>20){
                                        return false;
                                    }
                                   else{
                                        return true;
                                    }
                                }else{
                                    return true;
                                }
                            }
                        }
                    }
                },
            }
        });

    });

    $("#setting form").submit(function(ev){ev.preventDefault();});
    $("#submit").on("click", function(){
        var tip = trim($("#tip").val());
        var r = /^\+?[0-9]*$/;　　
        if(!r.test(tip)){
            alert("小费必须为----正整数");
            return;
        }
        var tip_remark = trim($("#tip_remark").val());
        if(tip>0){
            if(!tip_remark) {
                alert("小费备注不可为空");
                return;
            }
        }
        let shop_express_remark = $("#shop_express_remark").val();
        if(Gong.StringLength(shop_express_remark) > 200){
            alert("指派备注字数不可大于200");
            return false;
        }

        var bootstrapValidator = $("#setting").data('bootstrapValidator');
        console.log(bootstrapValidator)
        bootstrapValidator.validate();
        if(bootstrapValidator.isValid())
            $("#setting form").submit();
        else return;
    });
    function trim(str){
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
    var y = 2;
    var $company_id = '<?php  echo  $_SESSION['zp_shop']['company_id'];?>';
    function change(){
        if ($("#express_type").val()=="self"){
            if( y == 1){
                setTime(2);
            }
            y=2;
            // $("#pickup_time").hide();
            $(".self_rider").show();
            $("#shop_rider_fee").attr("required","required");
            $("#shop_delivery_time").prev().html('预计送达时间'); // 20200331
            $("#preinstall").html('预设抓"预计送达时间"不得早于收货时间');

            $("#jrd_type").hide();
            $("#shop_rider_fee").attr("required","");
            $("#sf_push_type").hide();
            $("#is_direct_delivery").hide();

        }else if( $("#express_type").val()=="dd"){
            if(y !== 1){
                setTime(1);
            }
            y=1;

            $("#shop_delivery_time").prev().html('发单时间');   // 20200331
            $("#preinstall").html('');
            // $("#preinstall").html('预设抓"预计送达时间"的前2小时');
            $(".self_rider").hide();
            $("#shop_rider_fee").attr("required","");

            $("#jrd_type").hide();
            $("#shop_rider_fee").attr("required","");
            $("#sf_push_type").hide();
            $("#is_direct_delivery").show();

        }else if($("#express_type").val()=="ss" ){
            if(y !== 1){
                setTime(1);
            }
            y=1;
            $("#shop_delivery_time").prev().html('取件时间');   // 20200331
            $("#preinstall").html('');
            $(".self_rider").hide();
            $("#shop_rider_fee").attr("required","");

            $("#jrd_type").hide();
            $("#shop_rider_fee").attr("required","");
            $("#sf_push_type").hide();
            $("#is_direct_delivery").hide();

        }else if($("#express_type").val()=="jrd"){
            $("#shop_delivery_time").prev().html('取件时间');   // 2020730
            $("#jrd_type").show();
            $("#shop_rider_fee").attr("required","required");
            $("#sf_push_type").hide();
            $("#is_direct_delivery").hide();

        }else if($("#express_type").val()=="uu" || $("#express_type").val()=="slw"){
            $("#shop_delivery_time").prev().html('取件时间');   // 2020730
            $("#sf_push_type").hide();
            $("#jrd_type").hide();
            $(".self_rider").hide();
            $("#is_direct_delivery").hide();

        }else{
            if( y == 1){
                setTime(2);
            }
            y=2;
            $("#shop_delivery_time").prev().html('预计送达时间'); // 20200331
            // $("#preinstall").html('预设抓"预计送达时间"不得早于收货时间');
            $(".self_rider").hide();
            $("#shop_rider_fee").attr("required","");

            $("#jrd_type").hide();
            $("#is_direct_delivery").hide();

            $("#shop_rider_fee").attr("required","");
            if($company_id == 1005){
                $("#sf_push_type").show();
            }

        }
    }

    function setTime(t) {
        var ot = $("#shop_delivery_time").val();
        var sdtime1 = new Date(ot);
        if(t == 1){
            var sdtime2 = sdtime1.setHours(sdtime1.getHours() -2);//小时
        }else {
            var sdtime2 = sdtime1.setHours(sdtime1.getHours() +2);//小时
        }
        $("#shop_delivery_time").val(formatDate(sdtime2));
    }
    function formatDate(now) {
        var sdtime1 = new Date(now);
        var year=sdtime1.getFullYear();  //取得4位数的年份
        var month=sdtime1.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
        var day=sdtime1.getDate();      //返回日期月份中的天数（1到31）
        var hour=sdtime1.getHours();     //返回日期中的小时数（0到23）
        var minute=sdtime1.getMinutes(); //返回日期中的分钟数（0到59）
        // var second=sdtime1.getSeconds(); //返回日期中的秒数（0到59）
        return year + '-' + (String(month).length > 1 ? month : '0' + month) + '-' +
            (String(day).length > 1 ? day : '0' + day) + ' ' + (String(hour).length > 1 ? hour : '0' + hour) + ':' + (String(minute).length > 1 ? minute : '0' + minute);
            // + ':' + (String(second).length > 1 ? second : '0' + second);
    }
</script>